package cn.com.bsfit.netty.resp;

import cn.com.bsfit.netty.config.properties.BaseNettyServerProperties;
import cn.com.bsfit.netty.utils.SpringBeanUtil;
import cn.com.bsfit.netty.utils.StringHelper;
import com.google.common.collect.Maps;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * 类名 BaseResponseUtils
 * 描述 公共返回数据的工具类
 *
 * @author hedonglin
 * @version 1.0
 * @date 2019/9/29 15:21
 */
public class BaseResponseUtils {
    public static Map<String, Object> successIsContains(boolean isJson, Map<String, Object> headMap,
                                                        Map<String, Object> bodyMap,
                                                        String currTopKey,
                                                        String currHeadKey,
                                                        String currBodyKey
    ) {
        Map<String, Object> result = Maps.newHashMap();
        if (isJson) {
            BaseNettyServerProperties properties = SpringBeanUtil.getBean(BaseNettyServerProperties.class);
            final boolean containsTop = properties.getJsonResponse().isContainsTop();
            final boolean containsHead = properties.getJsonResponse().isContainsHead();
            final boolean containsBody = properties.getJsonResponse().isContainsBody();

            // 包含
            if (containsTop){
                // 获取顶层标签为空
                if (StringHelper.isBlank(currTopKey)){
                    // 只返回Body和Head
                    if (containsBody && StringHelper.isNotBlank(currBodyKey)){
                        result.put(currBodyKey, bodyMap);
                    }
                    if (containsHead && StringHelper.isNotBlank(currHeadKey)){
                        result.put(currHeadKey, headMap);
                    }
                    return result;
                }else {
                    Map<String, Object> topMap = Maps.newHashMap();
                    // 只返回Body和Head
                    if (containsBody && StringHelper.isNotBlank(currBodyKey)){
                        topMap.put(currBodyKey, bodyMap);
                    }
                    if (containsHead && StringHelper.isNotBlank(currHeadKey)){
                        topMap.put(currHeadKey, headMap);
                    }
                    result.put(currTopKey, topMap);
                    return result;
                }
            }else {
                // 只返回Body和Head
                if (containsBody && StringHelper.isNotBlank(currBodyKey)){
                    result.put(currBodyKey, bodyMap);
                }
                if (containsHead && StringHelper.isNotBlank(currHeadKey)){
                    result.put(currHeadKey, headMap);
                }

                if (result.size() == 0){
                    result.putAll(headMap);
                    result.putAll(bodyMap);
                    return result;
                }
                return result;
            }
        }else {
            if (StringHelper.isNotBlank(currTopKey)) {
                Map<String, Object> topMap = Maps.newHashMap();
                if (StringHelper.isNotBlank(currHeadKey)) {
                    topMap.put(currHeadKey, headMap);
                } else {
                    topMap.putAll(headMap);
                }
                if (StringHelper.isNotBlank(currBodyKey)) {
                    topMap.put(currBodyKey, bodyMap);
                } else {
                    topMap.putAll(bodyMap);
                }
                result.put(currTopKey, topMap);
            } else {
                if (StringHelper.isNotBlank(currHeadKey)) {
                    result.put(currHeadKey, headMap);
                } else {
                    result.putAll(headMap);
                }
                if (StringHelper.isNotBlank(currBodyKey)) {
                    result.put(currBodyKey, bodyMap);
                } else {
                    result.putAll(bodyMap);
                }
            }
        }
        return result;
    }


}
